package com.photoeditor.demo.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.common.base.ui.widget.ripple.RippleEffect;

/**
 *
 */
public class RippleConstraintLayout extends ConstraintLayout implements RippleEffect.Callback {

    private boolean mNeedButtonCircle;
    private int mRippleColor;
    private boolean mEffectEnabled;
    private RippleEffect mEffect;
    private int mRadius;
    private RectF mRectF;

    public RippleConstraintLayout(Context context) {
        this(context, (AttributeSet) null);
    }

    public RippleConstraintLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mEffectEnabled = true;
        this.initPars(context, attrs);
        this.initEffect();
    }

    private void initPars(Context context, AttributeSet attrs) {
        this.mNeedButtonCircle = false;
        this.mRippleColor = 641618017;
        this.mRadius = -1;
    }

    private void initEffect() {
        this.mEffect = new RippleEffect(this.getContext());
        this.mEffect.setCallback(this);
        this.mEffect.setNeedDrawButtonCircle(this.mNeedButtonCircle);
        this.mEffect.setColor(this.mRippleColor);
        this.setLayerType(1, (Paint) null);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (this.mEffect != null) {
            this.mEffect.setBounds(0, 0, w, h);
        }

        this.mRectF = new RectF(0.0F, 0.0F, (float) w, (float) h);
    }

    public void setBackgroundDrawable(Drawable background) {
        super.setBackgroundDrawable(background);
        if (this.getBackground() != null && this.mEffect != null) {
            this.mEffect.setMask(this.getBackground());
        }

    }

    public boolean onTouchEvent(MotionEvent event) {
        boolean ret = super.onTouchEvent(event);
        int action = event.getAction();
        if ((action == 0 || action == 2) && this.mEffect != null && this.mEffectEnabled) {
            this.mEffect.setHotspot(event.getX(), event.getY());
        }

        return ret;
    }

    public void dispatchDraw(Canvas canvas) {
        this.clipRoundRectCanvas(canvas);
        if (this.mEffect != null && this.mEffectEnabled) {
            this.mEffect.draw(canvas);
        }

        super.dispatchDraw(canvas);
    }

    private void clipRoundRectCanvas(Canvas canvas) {
        if (this.mRadius > 0) {
            Path path = new Path();
            path.addRoundRect(this.mRectF, (float) this.mRadius, (float) this.mRadius, Path.Direction.CW);
            canvas.clipPath(path);
        }

    }

    public void refreshDrawableState() {
        super.refreshDrawableState();
        if (this.mEffect != null && this.mEffectEnabled) {
            this.mEffect.setState(this.getDrawableState());
        }

    }

    public void setEffectEnabled(boolean enabled) {
        this.mEffectEnabled = enabled;
    }

    public void setMask(int resId) {
        this.mEffect.setMask(this.getContext().getResources().getDrawable(resId));
    }

    public RippleEffect getEffect() {
        return this.mEffect;
    }

    public void setMask(Drawable mask) {
        this.mEffect.setMask(mask);
    }

    public void invalidateDrawable(RippleEffect who) {
        this.invalidate();
    }
}
